using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;

namespace Map
{
    
  

    public class Waypoints
    {
        
        Vector2 position = new Vector2(0.0f, 0.0f);
        int left = 99;
        int right = 99;
        int up = 99;
        int down = 99;
        
		
		//Accessor Functions
        public Waypoints()
        {
            left = 99;
            right = 99;
            up = 99;
            down = 99;
            position.X = 0.0f;
            position.Y = 0.0f;
        }
        
		
		public int Left 
        {
			get { return left; }
			set { left = value; }
        }
		public int Right
		{
			get { return right; }
			set { right = value; }
		}
		public int Up
		{
			get { return up; }
			set { up = value; }
		}
		public int Down
		{
			get { return down; }
			set { down = value; }
		}
		public Vector2 Position
		{
			get { return position; }
			set { position = value; }
		}

        
    }
    //=========================================
    //class SuperPellets
    //=========================================
    // extracted
    //================================================
    //Class regularPellets
    //================================================
    // extracted
    //================================================
    //Main class Map
    //================================================
    class Map
    {
        protected int Score, Lives;
        Texture2D background;
        Texture2D point;
        Waypoints tmp;
        List<Waypoints> currentWaypoints;
        List<SuperPellets> listOfBigPellets;
        List<regularPellet> listOfSmallPellets;
        StreamReader file;
        long frameCount = 0;
        long pressed = 0;
        bool drawWaypoints = false;
        Texture2D bigPellet;
        Texture2D smallPellet;
		bool ateSmallPellet = false;
		bool ateBigPellet = false;

		public bool IsEmpty
		{
			get { return (listOfBigPellets.Count == 0 && listOfSmallPellets.Count == 0); }

		}
		public bool AteSmallPellet
		{
			get { return ateSmallPellet; }
		}
		public bool AteBigPellet
		{
			get { return ateBigPellet; }
		}

        public Map(GraphicsDevice device)
        {
            tmp = new Waypoints();
            Score = 0;
            Lives = 3;
            
            listOfBigPellets = new List<SuperPellets>();
            listOfSmallPellets = new List<regularPellet>();
            LoadLevel1();
            setupPellets();
        }

        public void LoadContent(ContentManager content)
        {
            background = content.Load<Texture2D>("background1");
            point = content.Load<Texture2D>("point");
            bigPellet = content.Load<Texture2D>("pellet");
            smallPellet = content.Load<Texture2D>("smallPellet");

        }

        public void Update(float elapsed, Vector2 pacPosition)
        {
            
            CheckPelletCollision(pacPosition);
            
        }
        public void Render(ref SpriteBatch sprite)
        {
            
            frameCount++;
            if (KeyPress.KeyPressGame.KeyPressedThisFrame(Keys.F1) && pressed < frameCount-5)
            {
                pressed = frameCount;
                drawWaypoints = !drawWaypoints;
            }
            sprite.Draw(background, new Vector2(65.0f, 65.0f), new Rectangle(0, 0, 512,512), Color.White, 0.0f, new Vector2(16.0f, 16.0f), 1.0f, SpriteEffects.None, 0.1f);
            int x = 0;
            int y = 0;
            
            if (drawWaypoints)
            {
                foreach (Waypoints a in currentWaypoints)
                {
                    x = (int)a.Position.X;
                    y = (int)a.Position.Y;
                    sprite.Draw(point, new Rectangle(x - 8, y - 8, 16, 16), Color.White);

                }
            }
            foreach (SuperPellets a in listOfBigPellets)
            {
                x = (int)a.Position.X;
                y = (int)a.Position.Y;
                sprite.Draw(bigPellet, new Rectangle(x, y, 32, 32), Color.White);
            }
            foreach (regularPellet a in listOfSmallPellets)
            {
                x = (int)a.Position.X;
                y = (int)a.Position.Y;
                sprite.Draw(smallPellet, new Rectangle(x, y, 32, 32), Color.White);
            }
        }
        private void LoadLevel1()
        {
			file = new StreamReader("level1.txt");
            tmp = new Waypoints();
            currentWaypoints = new List<Waypoints>();
            tmp.Left = 8;
            tmp.Right = 47;
            tmp.Position = new Vector2(289, 385);
            currentWaypoints.Add(tmp);
            string nextString = file.ReadLine();
            if (nextString.Contains("Waypoint"))
            {
                while (nextString != "END")
                {
                    //Reset all positions
                    tmp = new Waypoints();
                    if (nextString.Contains("Vector"))
                    {
                        int x, y;
                        int beginningOfX = nextString.IndexOf('x') + 1;
                        int beginningOfY = nextString.IndexOf('y', beginningOfX) + 1;

                        x = Convert.ToInt32(nextString.Substring(beginningOfX, beginningOfY - beginningOfX - 1));
                        y = Convert.ToInt32(nextString.Substring(beginningOfY));
                        tmp.Position = new Vector2(x+48+16, y+48+16);
                    }
                    nextString = file.ReadLine();
                    int nextNode = 0;
                    while (!nextString.Contains("Waypoint") && nextString != "END")
                    {
                        if (nextString.Contains("Up"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(3));
                            tmp.Up = nextNode;
                        }
                        if (nextString.Contains("Down"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(5));
                            tmp.Down = nextNode;
                        }
                        if (nextString.Contains("Left"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(5));
                            tmp.Left = nextNode;
                        }
                        if (nextString.Contains("Right"))
                        {
                            nextNode = Convert.ToInt32(nextString.Substring(6));
                            tmp.Right = nextNode;
                        }
                        nextString = file.ReadLine();
                    }
                    currentWaypoints.Add(tmp);
                }//End while
            }//End if
            file.Close();
        }//End LoadLevel1
        public List<Waypoints> getWaypoints()
        {
            return currentWaypoints;
        }
        private void setupPellets()
        {
            SuperPellets tmp = new SuperPellets();
            listOfBigPellets = tmp.InitSuperPellets();
            regularPellet regPellet = new regularPellet();
            listOfSmallPellets = regPellet.InitRegularPellets();
        }

        
/*
        private List<SuperPellets> InitSuperPellets()
        {
            SuperPellets tmp = new SuperPellets();
            //Create four Super Pellets
            tmp.Position = new Vector2(65, 65);
            listOfBigPellets.Add(tmp);
            tmp = new SuperPellets();
            tmp.Position = new Vector2(512, 65);
            listOfBigPellets.Add(tmp);
            tmp = new SuperPellets();
            tmp.Position = new Vector2(65, 512);
            listOfBigPellets.Add(tmp);
            tmp = new SuperPellets();
            tmp.Position = new Vector2(512, 512);
            listOfBigPellets.Add(tmp);
        }
 * */
        private void CheckPelletCollision(Vector2 pacPosition)
        {
			ateSmallPellet = false; 
			ateBigPellet = false;
			int removeIndex = 999;
            pacPosition.X -= 16;
            pacPosition.Y -= 16;
            foreach (SuperPellets a in listOfBigPellets)
            {
                Vector2 tmp = a.Position;
                tmp -= pacPosition;
                if (tmp.Length() < 20.0f)
                    removeIndex = listOfBigPellets.IndexOf(a);
            }
            if (removeIndex < 999)
            {
                listOfBigPellets.RemoveAt(removeIndex);
				ateBigPellet = true;
                System.Console.WriteLine("Score: " + Score + "   Lives: " + Lives);
                Score += 30;
                return;
            }
            foreach (regularPellet a in listOfSmallPellets)
            {
                Vector2 tmp = a.Position;
                tmp -= pacPosition;
                if (tmp.Length() < 16.0f)
                {
                    removeIndex = listOfSmallPellets.IndexOf(a);
                    break;
                }
            }
			if (removeIndex < 999)
			{
				listOfSmallPellets.RemoveAt(removeIndex);
                Score += 5;
				ateSmallPellet = true;
                System.Console.WriteLine("Score: " + Score + "   Lives: " + Lives);
			}
            
        }
            
    }
}

